﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BYFW.SFL
{
    public class LogOP
    {
        public LogOP()
        {
        }

        private static LogOP s_log = null;
        private static object s_lock = new object();

        public static LogOP Log
        {
            get
            {
                lock (s_lock)
                {
                    if (s_log == null)
                    {
                        lock (s_lock)
                        {
                            s_log = new LogOP();
                        }
                    }
                    return s_log;
                }
            }
        }

        private LogType m_logType = LogType.Error;

        public LogType LogType
        {
            get { return m_logType; }
            set { m_logType = value; }
        }
        private string m_folderName = @"c:\Log";

        public string FolderName
        {
            get { return m_folderName; }
            set { m_folderName = value; }
        }
        private string m_template = "$datetime$[$logType$]:\t $msg$\r\n----------------------------------\r\n";

        public string Template
        {
            get { return m_template; }
            set { m_template = value; }
        }
        private int m_expirationDays = 7;

        public int ExpirationDays
        {
            get { return m_expirationDays; }
            set
            {
                if (value <= 0)
                {
                    value = 1;
                }
                m_expirationDays = value;
            }
        }

        private object m_lock = new object();

        /// <summary>
        /// 移除过期的日志文件
        /// </summary>
        public void RemoveExpirationLogFile()
        {
            if (Directory.Exists(m_folderName))
            {
                string[] files = Directory.GetFiles(m_folderName);
                foreach (var file in files)
                {
                    try
                    {
                        FileInfo fi = new FileInfo(file);
                        if (fi.CreationTime.AddDays(m_expirationDays) < DateTime.Now)
                        {
                            fi.Delete();
                        }
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="logType"></param>
        public void LogMsg(string msg, LogType logType)
        {
            if ((int)logType >= (int)m_logType)
            {
                try
                {
                    lock (m_lock)
                    {
                        if (!Directory.Exists(m_folderName))
                        {
                            Directory.CreateDirectory(m_folderName);
                        }
                        this.RemoveExpirationLogFile();
                        string fileName = (Path.Combine(m_folderName, "log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));
                        File.AppendAllText(fileName, msg);
                    }
                }
                catch { }
            }
        }
        /// <summary>
        /// 使用带格式的消息记录日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="logType"></param>
        public void LogFormatMsg(string msg, LogType logType)
        {
            string newMsg = m_template.Replace("$datetime$", DateTime.Now.ToString()).Replace("$logType$", logType.ToString()).Replace("$msg$", msg);
            LogMsg(newMsg, logType);
        }
        /// <summary>
        /// 记录Error类型的日志
        /// </summary>
        /// <param name="msg"></param>
        public void LogError(string msg)
        {
            this.LogFormatMsg(msg, LogType.Error);
        }
    }

    public enum LogType
    {
        Info,
        Debug,
        Warning,
        Error,
        Never,
    }
}
